package cn.sdut.thrift.server;

import cn.sdut.thrift.HelloWorldService;
import cn.sdut.thrift.service.HelloWorldImpl;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.transport.TServerSocket;

/**
 * @author MaJiatao
 * @description: 简单的单线程服务模型，一般用于测试
 * @created: 2019/10/22
 */
public class HelloServerDemo {
    public static final int SERVER_PORT = 8090;

    public void startServer() {
        try {
            System.out.println("HelloWorld TSimpleServer start");
            // 创建processor
            HelloWorldService.Processor<HelloWorldService.Iface> processor = new HelloWorldService.Processor<>(new HelloWorldImpl());
            // 创建transport
            TServerSocket transport = new TServerSocket(SERVER_PORT);
            // 创建protocol
            TBinaryProtocol.Factory protocol = new TBinaryProtocol.Factory();

            TServer.Args args = new TServer.Args(transport);
            args.processor(processor);
            args.protocolFactory(protocol);
            TSimpleServer tSimpleServer = new TSimpleServer(args);
            tSimpleServer.serve();
        }catch (Exception e) {
            System.out.println("Server start error!!!!");
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        HelloServerDemo server = new HelloServerDemo();
        server.startServer();
    }
}
